package com.uzai.console.service.dingding.impl;

import com.alibaba.fastjson.JSONObject;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.entity.DingExclusiveGroupConfig;
import com.uzai.console.entity.DingWarningGroup;
import com.uzai.console.entity.MerBaseConfig;
import com.uzai.console.mapper.DingWarningGroupMapper;
import com.uzai.console.mapper.MerBaseConfigMapper;
import com.uzai.console.service.dingding.DingDingCallBackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DingDingCallBackServiceImpl implements DingDingCallBackService {

   @Autowired
   private MerBaseConfigMapper merBaseConfigMapper;

    @Autowired
    private DingWarningGroupMapper dingWarningGroupMapper;

    @Override
    public void chatAddMember(String openConversationId) {
        MerBaseConfig merBaseConfig = merBaseConfigMapper.queryByVal(openConversationId);
        if(merBaseConfig==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "未找到相关群");
        }
        int nowTime = DateUtil.getNowTime();
        DingExclusiveGroupConfig dingExclusiveGroupConfig = JSONObject.parseObject(merBaseConfig.getVal(), DingExclusiveGroupConfig.class);
        dingExclusiveGroupConfig.setIfJoinGroup(1);
        String jsonString = JSONObject.toJSONString(dingExclusiveGroupConfig);
        merBaseConfig.setVal(jsonString);
        merBaseConfig.setUpdateTime(nowTime);
        merBaseConfigMapper.update(merBaseConfig);
    }


    @Override
    public void warningGroupAddMember(String openConversationId){
        DingWarningGroup dingWarningGroup= dingWarningGroupMapper.queryDingWarningGroupByConfig(openConversationId);
        if(dingWarningGroup==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "未找到相关群");
        }
        int nowTime = DateUtil.getNowTime();
        DingExclusiveGroupConfig dingExclusiveGroupConfig = JSONObject.parseObject(dingWarningGroup.getGroupConfig(), DingExclusiveGroupConfig.class);
        dingExclusiveGroupConfig.setIfJoinGroup(1);
        String jsonString=JSONObject.toJSONString(dingExclusiveGroupConfig);
        dingWarningGroup.setGroupConfig(jsonString);
        dingWarningGroup.setUpdateTime(nowTime);
        dingWarningGroupMapper.update(dingWarningGroup);
    }
}
